package com.changgou.controller;

import com.changgou.file.FastDFSFile;
import com.changgou.util.FastDFSClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author 李民康
 * @date 2022/1/12 17:01
 */
@RestController
@CrossOrigin
@Api(value = "file文件操作模块",tags = "文件上传微服务-测试文件上传功能")
@RequestMapping("/file")
public class TestFileController {
    /***
     * 文件上传
     * @return
     */
    @ApiOperation("测试文件上传")
    @PostMapping(value = "/upload")
    public String upload(@RequestParam("file") MultipartFile file) throws Exception {
        System.out.println("文件上传来了：：："+file);
        //封装一个FastDFSFile
        FastDFSFile fastDFSFile = new FastDFSFile(file.getOriginalFilename(), //文件名字
                file.getBytes(),            //文件字节数组
                StringUtils.getFilenameExtension(file.getOriginalFilename()));//文件扩展名

        //文件上传
        String[] uploads = FastDFSClient.upload(fastDFSFile);
        //组装文件上传地址
        return FastDFSClient.getTrackerUrl()+"/"+uploads[0]+"/"+uploads[1];
    }

    @ApiOperation("测试")
    @GetMapping("test")
    public Object test(String str) {
        return "我是你爹：："+str;
    }
}
